﻿package fwk.form.elements 
{
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class AbstractQuestion extends MovieClip
	{
		protected var _mc:MovieClip;
		protected var _id:String;
		protected var _value:String = "";
		protected var _type:String;
		protected var _tabReponses:Array;
		
		
		public var required:Boolean = false;
		public var testType:String = "";
		public var testParam:String = "";
		public var codeError:String = "";
		
		protected var _rollOver:Boolean = false;
		private var _spriteFocus:Sprite;
		private var _colorShapeFocus:int;
		private var _shapeFocus:Boolean = false;
		
		public function AbstractQuestion(mc:MovieClip = null) {
			
			if (mc != null) _mc = mc;
			else _mc = this;
			
			_spriteFocus = new Sprite();
			_colorShapeFocus = 0x000000;
			
			_tabReponses = new Array();
			
		}
		
		protected function _init(xmlConfig:XML)
		{
			
			_id 		= xmlConfig.@id;
			_value 		= xmlConfig.@value;
			_type 		= xmlConfig.@type;
			
		
			if(xmlConfig.@required is XMLList && xmlConfig.@required == "true") required = true;
			if(xmlConfig.@testType is XMLList) testType 	= xmlConfig.@testType;
			if(xmlConfig.@testParam is XMLList) testParam 	= xmlConfig.@testParam;
			if (xmlConfig.@codeError is XMLList) codeError 	= xmlConfig.@codeError;
			

			
			
			
		}
		public function get mc():MovieClip
		{
			return _mc;
		}
		public function get containerReponses():MovieClip
		{
			return null;
		}
		public function set containerReponses(clip:MovieClip)
		{

		}
		public function get value():String
		{
			return _value;
		}
		public function get id():String
		{
			return _id;
		}
		//--------------------------------------------------------
		public function set id(val:String)
		{
			_id = val;
		}
		public function get type():String
		{
			return _type;
		}
		//--------------------------------------------------------
		public function set type(val:String)
		{
			_type = val;
		}
		
		public function get reponse():AbstractReponse
		{
			return null;
		}

		
		public function get reponsesTab():Array
		{
			return _tabReponses;
		}
		public function set reponsesTab(val:Array):void
		{
			_tabReponses = val;
		}
		
		public function get rollOver():Boolean { return _rollOver; }
		
		public function set rollOver(value:Boolean):void 
		{
			if ( _rollOver != value) {
				_rollOver = value;
				
				if ( _rollOver) {
					_mc.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					_mc.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				}else {
					_mc.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					_mc.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				}
			}

			
		}
		
		public function get spriteFocus():Sprite { return _spriteFocus; }
		
		public function get colorShapeFocus():int { return _colorShapeFocus; }
		
		public function set colorShapeFocus(value:int):void 
		{
			_colorShapeFocus = value;
		}
		
		public function get shapeFocus():Boolean { return _shapeFocus; }
		
		public function set shapeFocus(value:Boolean):void 
		{
			_shapeFocus = value;
		}
		
		public function onMouseOut(e:MouseEvent):void 
		{
			if ( hasLabel("out") && _mc.currentLabel != "out")
			{
				_mc.gotoAndPlay("out");
			}
		}
		
		public function onMouseOver(e:MouseEvent):void 
		{
			if ( hasLabel("over") &&  _mc.currentLabel != "over")
			{
				_mc.gotoAndPlay("over");
			}
		}
		public function addReponse(rep:AbstractReponse)
		{
			_tabReponses.push(rep);
		}
		public function displayError()
		{
			trace("XXX Error in question " + this.id);
			
			if ( _mc.currentLabel != "error")
			{
				_mc.gotoAndPlay("error");
			}
		}
		public function hideError()
		{
			if ( _mc.currentLabel != "valid" &&  _mc.currentLabel != "over" && _mc.currentLabel != "out" && _mc.currentLabel != null)
			{
				_mc.gotoAndPlay("valid");
			}
			
		}
		public function hasLabel(label:String, clip:MovieClip = null):Boolean
		{
			
			var myClip:MovieClip = _mc;
			if ( clip != null) myClip = clip; 
			var nbLabel:int = myClip.currentLabels.length;
			for (var i:int = 0; i < nbLabel; i++) 
			{
				if ( myClip.currentLabels[i].name == label) {
					return true;
					break;
				}
			}
			
			return false;
			
		}
		public function dispose():void
		{
			
			
		}
		public function reset():void
		{

		}
	}
	
}